package com.xqboss.apps.controller.consumer;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.xqboss.apps.domain.consumer.ConsumerInteractMsg;
import com.xqboss.apps.dto.consumer.ConsumerInteractMsgDto;
import com.xqboss.apps.service.consumer.ConsumerInteractMsgService;
import com.xqboss.apps.vo.consumer.ConsumerInteractMsgVo;
import com.xqboss.common.annotation.RepeatSubmit;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 互动消息表接口
 *
 * @author wangcong
 * @date 2023-08-31
 */
@Api(tags = {"互动消息管理"})
@RestController
@RequestMapping("/apps/consumer/interact")
public class ConsumerInteractMsgController extends BaseController {

    @Autowired
    private ConsumerInteractMsgService consumerInteractMsgService;

    @ApiOperation("互动消息列表")
    @PostMapping("/list")
    public R<List<ConsumerInteractMsgVo>> listMe(@RequestBody ConsumerInteractMsgDto dto) {
        dto.setUserId(getUserId());
        LambdaQueryWrapper<ConsumerInteractMsg> wq = new LambdaQueryWrapper<>();
        wq.eq(ConsumerInteractMsg::getUserId, dto.getUserId());
        wq.orderByDesc(ConsumerInteractMsg::getCreateTime);
        dto.startPage();
        List<ConsumerInteractMsg> list = consumerInteractMsgService.list(wq);

        List<ConsumerInteractMsgVo> vos = BeanUtil.copyToList(list, ConsumerInteractMsgVo.class);
        return R.ok(vos, list);
    }

    @RepeatSubmit(interval = 1000, justUrl = true)
    @ApiOperation("编辑互动消息")
    @PutMapping("/update")
    public R update(@RequestBody ConsumerInteractMsgDto dto) {
        ConsumerInteractMsg msg = consumerInteractMsgService.getById(dto.getId());
        Objects.requireNonNull(msg, "资源不存在");
        ConsumerInteractMsg interactMsg = BeanUtil.copyProperties(dto, ConsumerInteractMsg.class);
        if (!consumerInteractMsgService.updateById(interactMsg)) {
            throw new ServiceException("编辑失败");
        }
        return R.ok();
    }

}
